package zju.ccnt.rest.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zju.ccnt.rest.api.ApiBusinessUser;
import zju.ccnt.rest.domain.BusinessUser;
import zju.ccnt.rest.repository.BusinessUserRepository;
import zju.ccnt.oauth2.user.UserService;
import zju.ccnt.oauth2.user.api.CreateUserRequest;
import zju.ccnt.oauth2.user.api.PasswordRequest;
import zju.ccnt.oauth2.user.exception.DuplicateUserException;
import zju.ccnt.oauth2.user.exception.InvalidUserException;

/**
 * Created by zha on 14/11/3.
 */
@Service
public class BusinessUserServiceImpl extends UserService<ApiBusinessUser>{
    private Logger LOG = LoggerFactory.getLogger(BusinessUserServiceImpl.class);

    private BusinessUserRepository businessUserRepository;

    @Autowired
    public BusinessUserServiceImpl(
            BusinessUserRepository businessUserRepository) {
        super();
        this.businessUserRepository = businessUserRepository;
    }

    @Override
    public void updateUserPasswordToDB(PasswordRequest newPassword, String email) {
        BusinessUser businessUser = businessUserRepository.findByEmail(email);
        if (businessUser != null) {
            businessUser.setPassword(passwordEncoder.encode(newPassword.getPassword()));
            businessUserRepository.save(businessUser);
        } else {
            LOG.info("No valid business_user email was given.");
            throw new InvalidUserException(email);
        }
    }

    @Override
    public ApiBusinessUser getUser(final String email) {
        BusinessUser businessUser = businessUserRepository.findByEmail(email);
        if (businessUser != null){
            return new ApiBusinessUser(businessUser);
        }
        LOG.info("BusinessUser [{}] does not exist.");
        return null;
    }

    @Override
    @Transactional
    public ApiBusinessUser createUserToDB(CreateUserRequest<ApiBusinessUser> createUserRequest) {
        final String email = createUserRequest.getUser().getEmail();
        if (businessUserRepository.findByEmail(email) == null) {
            LOG.info("BusinessUser does not already exist in the data store - creating a new user [{}].", email);
            BusinessUser newUser = insertNewBusinessUser(createUserRequest);
            LOG.debug("Created new user [{}] ", newUser.getName() + "@" + newUser.getName());
            //todo 创建商家用户表 根据存储方式而定（MONGODB MYSQL）
            return new ApiBusinessUser(newUser);
        } else {
            LOG.info("Duplicate user located, exception raised with appropriate HTTP response code.");
            throw new DuplicateUserException(email);
        }

    }

    private BusinessUser insertNewBusinessUser(CreateUserRequest request)
    {
        String hashedPassword = passwordEncoder.encode(request.getPassword().getPassword());
        ApiBusinessUser apiBusinessUser = (ApiBusinessUser)request.getUser();
        BusinessUser businessUser = new BusinessUser(apiBusinessUser, hashedPassword);
        return businessUserRepository.save(businessUser);
    }

}
